
$(document).ready(function(){

    if (window.localStorage.assign != "undefined" && window.localStorage.assign != undefined) {
        window.location.href='../html/home/home.html';
    }

    login();
});

/*点击登录按钮登录*/
function login(){
    $(".login-button").click(function(){
        var username=$(".username").val();
        var password=$(".password").val();

        callLoginServer(username, password);
    });
}

/*调用登录接口*/
function callLoginServer(username, password) {
    Api.login(username, password, function (result) {
        if (result.code == 200) {
            window.localStorage.assign = result.data.assigned;
            window.localStorage.token = result.data.token;
            window.localStorage.expired = result.data.expired;
            setupCompanyInfo();
            window.location.href='../html/home/home.html';
        } else if (result.code == 400) {
            alert("用户名或密码错误");
        }
        else {
            alert("登录失败");
        }
    }, function (error) {
        alert("登录失败" + error);
    });
}

function setupCompanyInfo() {
    Api.fetchCompanyDetailInfo(function (result) {
        window.localStorage.manager = result.data.companyName;
        window.localStorage.isProperty = result.data.propertyNature;
    }, function (error) {

    })
}

